package com.wzgl.core.webmagic.processor;

import java.io.Serializable;
import java.util.List;

import org.quartz.JobExecutionException;

import com.wzgl.core.utils.DateUtil;
import com.wzgl.core.webmagic.utils.PicUtiles;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;


/**
 * 获取分页中的内容
 */
public class HttpPageProcessor implements PageProcessor,Serializable {
    static String  cont = "";
    static String repex = "";
	private static final long serialVersionUID = 1L;
    private static Spider s ;
    private Site site = Site.me().setRetryTimes(1).setSleepTime(1000).addHeader("Accept-Encoding", "/");
	
    public static String getRepex() {
		return repex;
	}
	public static void setRepex(String repex) {
		HttpPageProcessor.repex = repex;
	}
	
	@Override
    public void process(Page page) {
//    	if(page.getUrl().regex("http://www.gamersky.com/handbook/"+date_str+"/").match()){ 
//    		String pic_text = page.getHtml().xpath("//div[@class='Mid2L_con']").toString();
//       	    //正文
//   		    cont = pic_text.substring(pic_text.indexOf("<p>"), pic_text.lastIndexOf("</p>")+4); 
//		}else if(page.getUrl().regex("http://www.gamersky.com/news/"+date_str+"/\\d+\\_").match()
//    			|| page.getUrl().regex("http://www.gamersky.com/hardware/"+date_str+"/\\d+\\_").match()
//	   			|| page.getUrl().regex("http://www.gamersky.com/review/"+date_str+"/\\d+\\_").match() 
//	   			|| page.getUrl().regex("http://www.gamersky.com/zl/column/"+date_str+"/\\d+\\_").match()){
//    		String pic_text = page.getHtml().xpath("//div[@class='Mid2L_con']").toString();
//       	    //正文
//    		cont = pic_text.substring(pic_text.indexOf("-->"), pic_text.lastIndexOf("</p>")+4);
//   		    
//    	}else if(page.getUrl().regex("http://www.ali213.net/news/html/\\d+\\-\\d+\\/\\d+\\_").match()){
//    		String pic_text = page.getHtml().xpath("//div[@class='n_show']").toString();
//       	    //正文
//    		cont = pic_text.substring(pic_text.indexOf("<p>"), pic_text.lastIndexOf("</p>")+4);
//    	}else if(page.getUrl().regex("http://gl.ali213.net/html/\\d+\\-\\d+\\/\\d+\\_").match()){
//    		String pic_text = page.getHtml().xpath("//div[@class='glzjshow_con']").toString();
//       	    //正文
//    		cont = pic_text.substring(pic_text.indexOf("<p>"), pic_text.lastIndexOf("</p>")+4);
//    	}
    	String pic_text = page.getHtml().xpath(getRepex()).toString();
    	if(pic_text == null || "".equals(pic_text)){
    		pic_text = page.getHtml().xpath("//div[@class='MidLcon']").toString();
    	}
   	    //正文
    	if(page.getUrl().regex("http://www.yxdown.com/gonglue").match()){
    		cont = pic_text.substring(0, pic_text.lastIndexOf("</p>")+4);
    	}else{
    		cont = pic_text.substring(pic_text.indexOf("<p"), pic_text.lastIndexOf("</p>")+4);
    	}
    	 //下载图片
		 List<String> pic_urls = page.getHtml().xpath(getRepex()+"/p/img").all();
		 List<String> pic_hrefs = page.getHtml().xpath(getRepex()+"/p/a").all();
		 //替换所有的图片链接和图片地址
		 if(pic_urls.size()<=0){
			 
		 }else{
			 //替换所有的图片链接和图片地址
    		 cont = PicUtiles.replaceAllHrefAndImg(pic_hrefs,pic_urls,cont);
		 }
    }
    @Override
    public Site getSite() {
        return site;
    }
	public static String execute(String herf,String r) throws JobExecutionException {
		// TODO Auto-generated method stub
		s = Spider.create(new HttpPageProcessor()).addUrl(herf).thread(1);  
		setRepex(r);
    	s.run();
    	return cont;
	}
}